package net.xdclass.util;

import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * @author whg
 * @date 2025-05-15
 * @description
 */
public class MD5 {
    public MD5() {
    }

    public static synchronized String md5_32(String plainText) {
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            md.update(plainText.getBytes(StandardCharsets.UTF_8));
            byte[] b = md.digest();
            StringBuffer buf = new StringBuffer();

            for(int offset = 0; offset < b.length; ++offset) {
                int i = b[offset];
                if (i < 0) {
                    i += 256;
                }

                if (i < 16) {
                    buf.append("0");
                }

                buf.append(Integer.toHexString(i));
            }

            return buf.toString();
        } catch (NoSuchAlgorithmException var6) {
            NoSuchAlgorithmException e = var6;
            e.printStackTrace();
            return null;
        }
    }

    public static synchronized String md5_16(String plainText) {
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            md.update(plainText.getBytes(StandardCharsets.UTF_8));
            byte[] b = md.digest();
            StringBuffer buf = new StringBuffer();

            for(int offset = 0; offset < b.length; ++offset) {
                int i = b[offset];
                if (i < 0) {
                    i += 256;
                }

                if (i < 16) {
                    buf.append("0");
                }

                buf.append(Integer.toHexString(i));
            }

            return buf.toString().substring(8, 24);
        } catch (NoSuchAlgorithmException var6) {
            NoSuchAlgorithmException e = var6;
            e.printStackTrace();
            return null;
        }
    }

    public static synchronized String md5_32(String plainText, String code) {
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            md.update(plainText.getBytes(code));
            byte[] b = md.digest();
            StringBuffer buf = new StringBuffer();

            for(int offset = 0; offset < b.length; ++offset) {
                int i = b[offset];
                if (i < 0) {
                    i += 256;
                }

                if (i < 16) {
                    buf.append("0");
                }

                buf.append(Integer.toHexString(i));
            }

            return buf.toString();
        } catch (NoSuchAlgorithmException var7) {
            var7.printStackTrace();
            return null;
        } catch (UnsupportedEncodingException var8) {
            UnsupportedEncodingException e = var8;
            e.printStackTrace();
            return null;
        }
    }

    public static synchronized String md5_16(String plainText, String code) {
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            md.update(plainText.getBytes(code));
            byte[] b = md.digest();
            StringBuffer buf = new StringBuffer();

            for(int offset = 0; offset < b.length; ++offset) {
                int i = b[offset];
                if (i < 0) {
                    i += 256;
                }

                if (i < 16) {
                    buf.append("0");
                }

                buf.append(Integer.toHexString(i));
            }

            return buf.toString().substring(8, 24);
        } catch (NoSuchAlgorithmException var7) {
            var7.printStackTrace();
            return null;
        } catch (UnsupportedEncodingException var8) {
            UnsupportedEncodingException e = var8;
            e.printStackTrace();
            return null;
        }
    }
}
